package rest

import (
	"strconv"

	"github.com/labstack/echo/v4"

	"bilinadmin/pkg/lueerror"

	pb "bilinadmin/api/pb/server"

	"bilinadmin/internal/service"
)

type permHandler struct {
	*Base
	name string
}

// NewPermissionHandler 品牌 handler
func NewPermissionHandler() *permHandler {
	return &permHandler{
		Base: base,
		name: "rest_role_handler",
	}
}

// Register 注册路由
func (h *permHandler) Register(r *echo.Group) {
	// 创建默认权限
	r.POST("/v1/default/create", h.createDefaultRoles)
	// 获取权限列表
	r.GET("/v1/list", h.getRoleList)
	// 创建权限
	r.POST("/v1/create", h.createRole)
	// 更新权限
	r.PUT("/v1/update", h.updateRole)
	// 消息发送（暂时放这)
	r.GET("/v1/message/send", h.sendMessage)
	// 获取权限列表
	r.GET("/v1/permission/list", h.getPermissions)
	// 获取角色详情
	r.GET("/v1/:role_id", h.getRoleInfo)
}

func (h *permHandler) createDefaultRoles(c echo.Context) error {
	ctx := c.Request().Context()
	type param struct {
		BrandId int64 `json:"brand_id" validate:"required"`
		StoreId int64 `json:"store_id"`
	}
	req := &param{}
	if errL := h.ParseBody(c, req); errL != nil {
		return errL
	}
	errL := service.GetRolePermissionSrv().CreateDefaultRoles(ctx, req.BrandId, req.StoreId)
	return h.SimpleRet(c, nil, errL)
}

func (h *permHandler) getRoleList(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := h.PermissionCheck(c, "admin")
	if errL != nil {
		return errL
	}
	storeId, errL := parseStoreId(c, false)
	if errL != nil {
		return errL
	}
	ret, errL := service.GetRolePermissionSrv().GetEmpRoleList(ctx, token.BrandId, storeId)
	return h.SimpleRet(c, ret, errL)
}

func (h *permHandler) createRole(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := h.PermissionCheck(c, "admin")
	if errL != nil {
		return errL
	}
	req := &pb.EmployeeRoleCreateReq{}
	if errL = h.ParseBody(c, req); errL != nil {
		return errL
	}
	req.StoreId, errL = parseStoreId(c, false)
	if errL != nil {
		return errL
	}
	ret, errL := service.GetRolePermissionSrv().CreateEmpRole(ctx, token.BrandId, req, token.EmpId)
	return h.SimpleRet(c, ret, errL)
}

func (h *permHandler) updateRole(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := h.PermissionCheck(c, "admin")
	if errL != nil {
		return errL
	}
	req := &pb.EmployeeRoleUpdateReq{}
	if errL = h.ParseBody(c, req); errL != nil {
		return errL
	}
	req.StoreId, errL = parseStoreId(c, false)
	if errL != nil {
		return errL
	}
	ret, errL := service.GetRolePermissionSrv().UpdateEmpRole(ctx, token.BrandId, req, token.EmpId)
	return h.SimpleRet(c, ret, errL)
}

func (h *permHandler) sendMessage(c echo.Context) error {
	ctx := c.Request().Context()
	msgId := c.QueryParam("msg_id")
	mId, err := strconv.ParseInt(msgId, 10, 64)
	if err != nil {
		return lueerror.InvalidRequest(err)
	}
	if mId == 0 {
		return nil
	}
	return service.GetRolePermissionSrv().SendMsgById(ctx, mId)
}

func (h *permHandler) getPermissions(c echo.Context) error {
	ctx := c.Request().Context()
	storeId, errL := parseStoreId(c, false)
	if errL != nil {
		return errL
	}
	ret, errL := service.GetRolePermissionSrv().GetPermissions(ctx, storeId)
	return h.SimpleRet(c, ret, errL)
}

func (h *permHandler) getRoleInfo(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := h.PermissionCheck(c, "admin")
	if errL != nil {
		return errL
	}
	roleId := c.Param("role_id")
	rId, err := strconv.ParseInt(roleId, 10, 64)
	if err != nil {
		return lueerror.InvalidRequest(err)
	}
	storeId, errL := parseStoreId(c, false)
	if errL != nil {
		return errL
	}
	ret, errL := service.GetRolePermissionSrv().GetRoleInfoById(ctx, token.BrandId, rId, storeId)
	return h.SimpleRet(c, ret, errL)
}
